package com.bfmet.crawler;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.ScriptTag;
import org.htmlparser.util.NodeList;

import com.bfmet.entity.News;

public class HotSpider extends Spider {

	@Override
	public void run() {
		this.getHot("http://www.cnmn.com.cn");
	}

	private void getHot(String url) {
		this.log("Fetch hot begin..." + url);
		try {
			Parser p = new Parser(url);
			p.setEncoding("utf-8");
			NodeList divList = p.parse(new AndFilter(new TagNameFilter("div"),
					new HasAttributeFilter("id", "focusLeft")));
			if (divList.elementAt(0).getChildren().elementAt(1) instanceof ScriptTag) {
				ScriptTag st = (ScriptTag) divList.elementAt(0).getChildren()
						.elementAt(1);
				this.parseJSParameter(st.getScriptCode());
			}

		} catch (Exception ex) {
			this.logException(ex);
		}
		log("Fetch hot end..." + url);
	}

	public void parseJSParameter(String jscode) {
		String[] pics = {}, links = {}, texts = {};
		Pattern pattern = Pattern.compile("var .*? = '.*?'");
		Matcher matcher = pattern.matcher(jscode);
		while (matcher.find()) {
			String result = matcher.group();
			Pattern p2 = Pattern.compile("'.*?'");
			if (result.indexOf("pics") != -1) {
				Matcher m2 = p2.matcher(result);
				while (m2.find()) {
					pics = m2.group().split("\\|");
				}
			}
			if (result.indexOf("links") != -1) {
				Matcher m2 = p2.matcher(result);
				while (m2.find()) {
					links = m2.group().split("\\|");
				}
			}
			if (result.indexOf("texts") != -1) {
				Matcher m2 = p2.matcher(result);
				while (m2.find()) {
					texts = m2.group().split("\\|");
				}
			}
		}
		for (int i = 0; i < pics.length; i++) {
			String pic = pics[i];
			String link = links[i];
			String text = texts[i];
			News news = new News();
			if (text.length() > 15) {
				text = text.substring(0, 15)+" ...";
			}
			news.setName(text.replaceAll("\\'", ""));
			news.setImageUrl(pic.replaceAll("\\'", ""));
			news.setContent(getContent(link.replaceAll("\\'", "")));
			news.setType(News.Type.HOT);
			this.save(news);
		}
	}

	private String getContent(String subUrl) {
		try {
			Parser p = new Parser(subUrl);
			p.setEncoding("utf-8");
			NodeList divList = p.parse(new AndFilter(new TagNameFilter("div"),
					new HasAttributeFilter("id", "content")));
			if (divList.elementAt(0) instanceof Div) {
				Div div = (Div) divList.elementAt(0);
				return div.toPlainTextString();
			}

		} catch (Exception ex) {
			this.logException(ex);
		}
		return null;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HotSpider hs = new HotSpider();
		hs.run();
	}

}
